import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;


public class AnaPencere extends JFrame
{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldAd;
	private JTextField textFieldSoyad;
	private JTextField textFieldBakiye;
	private JEditorPane editorPane;

	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					AnaPencere frame = new AnaPencere();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnaPencere()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblAd = new JLabel("Ad\u0131:");
		GridBagConstraints gbc_lblAd = new GridBagConstraints();
		gbc_lblAd.insets = new Insets(0, 0, 5, 5);
		gbc_lblAd.anchor = GridBagConstraints.EAST;
		gbc_lblAd.gridx = 0;
		gbc_lblAd.gridy = 0;
		contentPane.add(lblAd, gbc_lblAd);
		
		textFieldAd = new JTextField();
		GridBagConstraints gbc_textFieldAd = new GridBagConstraints();
		gbc_textFieldAd.gridwidth = 2;
		gbc_textFieldAd.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldAd.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldAd.gridx = 1;
		gbc_textFieldAd.gridy = 0;
		contentPane.add(textFieldAd, gbc_textFieldAd);
		textFieldAd.setColumns(10);
		
		JLabel lblSoyad = new JLabel("Soyad:");
		GridBagConstraints gbc_lblSoyad = new GridBagConstraints();
		gbc_lblSoyad.anchor = GridBagConstraints.EAST;
		gbc_lblSoyad.insets = new Insets(0, 0, 5, 5);
		gbc_lblSoyad.gridx = 3;
		gbc_lblSoyad.gridy = 0;
		contentPane.add(lblSoyad, gbc_lblSoyad);
		
		textFieldSoyad = new JTextField();
		GridBagConstraints gbc_textFieldSoyad = new GridBagConstraints();
		gbc_textFieldSoyad.gridwidth = 3;
		gbc_textFieldSoyad.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldSoyad.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldSoyad.gridx = 4;
		gbc_textFieldSoyad.gridy = 0;
		contentPane.add(textFieldSoyad, gbc_textFieldSoyad);
		textFieldSoyad.setColumns(10);
		
		JLabel lblBakiye = new JLabel("Bakiye:");
		GridBagConstraints gbc_lblBakiye = new GridBagConstraints();
		gbc_lblBakiye.anchor = GridBagConstraints.EAST;
		gbc_lblBakiye.insets = new Insets(0, 0, 5, 5);
		gbc_lblBakiye.gridx = 7;
		gbc_lblBakiye.gridy = 0;
		contentPane.add(lblBakiye, gbc_lblBakiye);
		
		textFieldBakiye = new JTextField();
		GridBagConstraints gbc_textFieldBakiye = new GridBagConstraints();
		gbc_textFieldBakiye.insets = new Insets(0, 0, 5, 0);
		gbc_textFieldBakiye.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldBakiye.gridx = 8;
		gbc_textFieldBakiye.gridy = 0;
		contentPane.add(textFieldBakiye, gbc_textFieldBakiye);
		textFieldBakiye.setColumns(10);
		
		JButton btnEkle = new JButton("Ekle");
		btnEkle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ekle();
			}
		});
		GridBagConstraints gbc_btnEkle = new GridBagConstraints();
		gbc_btnEkle.gridwidth = 2;
		gbc_btnEkle.insets = new Insets(0, 0, 5, 5);
		gbc_btnEkle.gridx = 0;
		gbc_btnEkle.gridy = 1;
		contentPane.add(btnEkle, gbc_btnEkle);
		
		JButton btnAra = new JButton("Ara");
		btnAra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ara();
			}
		});
		GridBagConstraints gbc_btnAra = new GridBagConstraints();
		gbc_btnAra.gridwidth = 2;
		gbc_btnAra.insets = new Insets(0, 0, 5, 5);
		gbc_btnAra.gridx = 2;
		gbc_btnAra.gridy = 1;
		contentPane.add(btnAra, gbc_btnAra);
		
		JButton btnDzenle = new JButton("D\u00FCzenle");
		btnDzenle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				duzenle();
			}
		});
		GridBagConstraints gbc_btnDzenle = new GridBagConstraints();
		gbc_btnDzenle.gridwidth = 2;
		gbc_btnDzenle.insets = new Insets(0, 0, 5, 5);
		gbc_btnDzenle.gridx = 4;
		gbc_btnDzenle.gridy = 1;
		contentPane.add(btnDzenle, gbc_btnDzenle);
		
		JButton btnSil = new JButton("Sil");
		btnSil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sil();
			}
		});
		GridBagConstraints gbc_btnSil = new GridBagConstraints();
		gbc_btnSil.gridwidth = 2;
		gbc_btnSil.insets = new Insets(0, 0, 5, 5);
		gbc_btnSil.gridx = 6;
		gbc_btnSil.gridy = 1;
		contentPane.add(btnSil, gbc_btnSil);
		
		JButton btnListele = new JButton("Listele");
		btnListele.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listele();
			}
		});
		GridBagConstraints gbc_btnListele = new GridBagConstraints();
		gbc_btnListele.insets = new Insets(0, 0, 5, 0);
		gbc_btnListele.gridx = 8;
		gbc_btnListele.gridy = 1;
		contentPane.add(btnListele, gbc_btnListele);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 9;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 2;
		contentPane.add(scrollPane, gbc_scrollPane);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		editorPane = new JEditorPane();
		editorPane.setEditable(false);
		scrollPane.setViewportView(editorPane);
	}

	public void ekle()
	{
		if(textFieldAd.getText().isEmpty()    ||
		   textFieldSoyad.getText().isEmpty() ||
		   textFieldBakiye.getText().isEmpty()||
		   !tireKontrol(textFieldAd.getText())||
		   !tireKontrol(textFieldSoyad.getText()) ||
		   !bakiyeKontrol(textFieldBakiye.getText()))
			editorPane.setText("Islem basarisiz! Bosluklari kontrol edin!");
		else
		{
			Account a = new Account();
			a.setUserName(textFieldAd.getText().trim());
			a.setUserSurname(textFieldSoyad.getText().trim());
			a.setBalance(Double.parseDouble(textFieldBakiye.getText().trim()));
			Function.ekle(a);
		}
	}
	
	public void ara()
	{
		editorPane.setText("");

		if(textFieldAd.getText().isEmpty()    ||
		   textFieldSoyad.getText().isEmpty() ||
		   !tireKontrol(textFieldAd.getText()) ||
		   !tireKontrol(textFieldSoyad.getText()))
			editorPane.setText("Islem basarisiz! Bosluklari kontrol edin!");
		else
		{
			Vector<Account> gelen;
			Account a = new Account();
			a.setUserName(textFieldAd.getText().trim());
			a.setUserSurname(textFieldSoyad.getText().trim());
			gelen = Function.ara(a);
			
			if(gelen.size() == 0)
				editorPane.setText("Kayit Bulunamadi!");
			else
				for(int i = 0; i < gelen.size(); i++)
				{
					editorPane.setText(editorPane.getText() +
							"Kisi: " + gelen.get(i).getUserName() + " " + gelen.get(i).getUserSurname() + '\n' +
							"Bakiye: " + gelen.get(i).getBalance() + "\n\n");
				}
		}
	}

	public void listele()
	{
		editorPane.setText("");
		
		Vector<Account> gelen;
		gelen = Function.listele();
		
		for(int i = 0; i < gelen.size(); i++)
		{
			editorPane.setText(editorPane.getText() +
					"Kisi: " + gelen.get(i).getUserName() + " " + gelen.get(i).getUserSurname() + '\n' +
					"Bakiye: " + gelen.get(i).getBalance() + "\n\n");
		}

	}
	
	public void sil()
	{
		if(textFieldAd.getText().isEmpty()    ||
				   textFieldSoyad.getText().isEmpty() ||
				   textFieldBakiye.getText().isEmpty()||
				   !tireKontrol(textFieldAd.getText())||
				   !tireKontrol(textFieldSoyad.getText()) ||
				   !bakiyeKontrol(textFieldBakiye.getText()))
					editorPane.setText("Islem basarisiz! Bosluklari kontrol edin!");
				else
				{
					Account a = new Account();
					a.setUserName(textFieldAd.getText().trim());
					a.setUserSurname(textFieldSoyad.getText().trim());
					a.setBalance(Double.parseDouble(textFieldBakiye.getText().trim()));
					Function.sil(a);
				}
	}
	
	public void duzenle()
	{
		if(textFieldAd.getText().isEmpty()    ||
				   textFieldSoyad.getText().isEmpty() ||
				   textFieldBakiye.getText().isEmpty()||
				   !tireKontrol(textFieldAd.getText())||
				   !tireKontrol(textFieldSoyad.getText()) ||
				   !bakiyeKontrol(textFieldBakiye.getText()))
					editorPane.setText("Islem basarisiz! Bosluklari kontrol edin!");
				else
				{
					Account a = new Account();
					a.setUserName(textFieldAd.getText().trim());
					a.setUserSurname(textFieldSoyad.getText().trim());
					a.setBalance(Double.parseDouble(textFieldBakiye.getText().trim()));
					Function.duzenle(a);
				}
		
	}
	
	public boolean tireKontrol(String s)
	{
		boolean durum = true;
		
		for(int i = 0; i < s.toCharArray().length; i++)
			if(s.toCharArray()[i] == '-')
				durum = false;
		
		return durum;
	}
	
	public boolean bakiyeKontrol(String s)
	{
		boolean durum = true;
		
		for(int i = 0; i < s.toCharArray().length; i++)
			if(!(Character.isDigit(s.toCharArray()[i]) || s.toCharArray()[i] == '.'))
				durum = false;

		return durum;
	}

}

